﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FluentDateTime;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace de.codingfreaks.libs.cfDateTime.Test
{
    /// <summary>
    /// Contains test-methods for the <see cref="DateTimeSpan"/>-type.
    /// </summary>
    [TestClass]
    public class DateTimeSpanTest
    {

        #region member vars

        private TestContext m_tconThis;

        #endregion

        #region constructor

        public DateTimeSpanTest()
        {
        }

        #endregion

        #region properties

        /// <summary>
        /// Gets/sets informations about the current test and its state.        
        ///</summary>
        public TestContext TestContext
        {
            get { return m_tconThis; }
            set { m_tconThis = value; }
        }

        #endregion

        #region tests

        /// <summary>
        /// Tests for the different constructors available.
        /// </summary>
        [TestMethod,
         Owner("Alexander Schmidt"),
         Description("Tests for the different constructors available.")]
        public void ConstructorTest()
        {            
            // test of the parameter-less default-constructor
            DateTimeSpan dts1 = new DateTimeSpan();
            Assert.AreEqual(true, dts1.EnsureCompleteDays);
            Assert.AreEqual(true, dts1.DateFrom.HasValue);
            Assert.AreEqual(true, dts1.DateTo.HasValue);
            Assert.AreEqual(true, dts1.HasSpan);
            // test of the constructor taking the from- and to-date
            DateTime dtmThis = DateTime.Now;
            DateTimeSpan dts2 = new DateTimeSpan(dtmThis, dtmThis);
            Assert.AreEqual(false, dts2.EnsureCompleteDays);
            Assert.AreEqual(true, dts2.DateFrom.HasValue);
            Assert.AreEqual(true, dts2.DateTo.HasValue);
            Assert.AreEqual(false, dts2.HasSpan);
            // test of the constructor taking a year
            DateTimeSpan dts3 = new DateTimeSpan(2010);
            Assert.AreEqual(new DateTime(2010, 1, 1).BeginningOfDay(), dts3.DateFrom.Value);
            Assert.AreEqual(new DateTime(2010, 12, 31).EndOfDay(), dts3.DateTo.Value);
            Assert.AreEqual(true, dts3.EnsureCompleteDays);
            // test of constructor taking a month and a year
            DateTimeSpan dts4 = new DateTimeSpan(2010, 3);
            Assert.AreEqual(new DateTime(2010, 3, 1).BeginningOfDay(), dts4.DateFrom.Value);
            Assert.AreEqual(new DateTime(2010, 3, 31).EndOfDay(), dts4.DateTo.Value);
            Assert.AreEqual(true, dts4.EnsureCompleteDays);
            Assert.AreEqual(true, dts4.LaysInside(new DateTime(2010, 3, 12)));
        }

        /// <summary>
        /// Tests for the method LaysInside.
        /// </summary>
        [TestMethod,
         Owner("Alexander Schmidt"),
         Description("Tests for the method LaysInside.")]
        public void LaysInsideTest()
        {
            DateTimeSpan dtsThis = new DateTimeSpan(2010);
            Assert.AreEqual(true, dtsThis.LaysInside(new DateTime(2010, 4, 1)));
            Assert.AreEqual(true, dtsThis.LaysInside(new DateTime(2010, 12, 31)));
            Assert.AreEqual(true, dtsThis.LaysInside(new DateTime(2010, 1, 1)));
            Assert.AreEqual(true, dtsThis.LaysInside(new DateTime(2010, 2, 28)));
            Assert.AreEqual(false, dtsThis.LaysInside(new DateTime(2009, 12, 31)));
            Assert.AreEqual(false, dtsThis.LaysInside(new DateTime(2011, 1, 1)));
            Assert.AreEqual(false, dtsThis.LaysInside(new DateTime(2300, 6, 3)));
        }

        /// <summary>
        /// Tests for the property HasSpan.
        /// </summary>
        [TestMethod,
         Owner("Alexander Schmidt"),
         Description("Tests for the property HasSpan.")]
        public void HasSpanTest()
        {
            DateTimeSpan dtsThis = new DateTimeSpan(2010);
            Assert.AreEqual(true, dtsThis.HasSpan);
            dtsThis = new DateTimeSpan(2012, 3);
            Assert.AreEqual(true, dtsThis.HasSpan);
            DateTime dtmThis = DateTime.Now;
            dtsThis = new DateTimeSpan(dtmThis, dtmThis);
            Assert.AreEqual(false, dtsThis.HasSpan);
        }

        /// <summary>
        /// Tests for the property Days.
        /// </summary>
        [TestMethod,
         Owner("Alexander Schmidt"),
         Description("Tests for the property Days.")]
        public void DaysTest()
        {
            DateTimeSpan dtsThis = new DateTimeSpan(2010);
            Assert.AreEqual(365, dtsThis.Days.Count);
            Assert.AreEqual(true, dtsThis.Days.Contains(new DateTime(2010, 3, 1)));
            Assert.AreEqual(false, dtsThis.Days.Contains(new DateTime(2009, 12, 31)));
        }

        /// <summary>
        /// Tests for the property EnsureCompleteDays.
        /// </summary>
        [TestMethod,
         Owner("Alexander Schmidt"),
         Description("Tests for the property EnsureCompleteDays.")]
        public void EnsureCompleteDaysTest()
        {
            DateTimeSpan dtsThis = new DateTimeSpan(2010);
            dtsThis.EnsureCompleteDays = true;
            Assert.AreEqual(365 * 24 * 60 * 60 - 1, (int)dtsThis.CompleteTimeSpan.TotalSeconds);
            dtsThis = new DateTimeSpan(DateTime.Now, DateTime.Now);
            dtsThis.EnsureCompleteDays = true;
            Assert.AreEqual(24 * 60 * 60 - 1, (int)dtsThis.CompleteTimeSpan.TotalSeconds);
        }

        #endregion

    }
}
